package com.tg.paw.common.scheduling;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tg.paw.common.mq.entity.MqMessage;
import com.tg.paw.common.mq.entity.MqMessageLog;
import com.tg.paw.common.mq.service.IMqMessageLogService;
import com.tg.paw.common.mq.service.IMqMessageService;
import com.tg.paw.common.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 超期消息删除
 * @author Rubble
 * @date 2020/6/22
 */
//@Component
public class MqMessageScheduling {

    @Autowired
    private IMqMessageService mqMessageService;
    @Autowired
    private IMqMessageLogService mqMessageLogService;

    private static  final int day = 7;

//    @Scheduled(cron = "0 0 2 * * ?")
    public void deleteOutTimeMessage(){
        Date date  = DateUtil.offsetDay(new Date(), -7);
        LambdaQueryWrapper<MqMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(MqMessage::getCreateTime,date);
        List<MqMessage> mqMessages = mqMessageService.list(queryWrapper);
        mqMessageService.remove(queryWrapper);
        for (MqMessage item :
                mqMessages) {
            MqMessageLog messageLog = MessageUtil.buildMessageLog(item);
            messageLog.setRemark("超期删除 by task");
            mqMessageLogService.save(messageLog);
        }

    }
}
